package org.jeecg.tender.keyword.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.JacksonUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.tender.keyword.entity.TenderSecurityKeywordManagement;
import org.jeecg.tender.keyword.mapper.TenderSecurityKeywordManagementMapper;
import org.jeecg.tender.keyword.service.ITenderSecurityKeywordManagementService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: tender_security_keyword_management
 * @Author: jeecg-boot
 * @Date:   2025-02-26
 * @Version: V1.0
 */
@Service
public class TenderSecurityKeywordManagementServiceImpl extends ServiceImpl<TenderSecurityKeywordManagementMapper, TenderSecurityKeywordManagement> implements ITenderSecurityKeywordManagementService {

    private static final String SECURITY_KEYWORD_MANAGE_CACHE = "SECURITY_KEYWORD_MANAGE_CACHE";
    private static final long SECURITY_KEYWORD_MANAGE_CACHE_EXPIRE = 3600L;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public Map<Long, String> getCacheSecurityKeywordManagement() {
        if (redisUtil.hasKey(SECURITY_KEYWORD_MANAGE_CACHE)) {
            String s = JacksonUtil.toJsonString(redisUtil.get(SECURITY_KEYWORD_MANAGE_CACHE));
            return JacksonUtil.parseObject(s, new TypeReference<Map<Long, String>>() { });
        } else {
            Map<Long, String> security = super.list().stream().collect(Collectors.toMap(TenderSecurityKeywordManagement::getId, TenderSecurityKeywordManagement::getKeywordName));
            // 缓存
            redisUtil.set(SECURITY_KEYWORD_MANAGE_CACHE, security, SECURITY_KEYWORD_MANAGE_CACHE_EXPIRE);
            return security;
        }
    }
}
